# !/usr/bin/python3
# -*- coding: utf-8 -*-

"""
角色和部门关联表控制器类
----------------------------------------------------
@Project :   xinhou-openai-framework
@File    :   RoleDept.py
@Contact :   sp_hrz@qq.com

@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2023/04/16 22:04  peng.shen   v1.0.0     None
"""

from fastapi import APIRouter, Depends
from fastapi.params import Path
from loguru import logger
from sqlalchemy.orm import Session

from apps.admin.schema.RoleDeptSchema import ReqRoleDeptUpdateSchema, ResRoleDeptDetailSchema, \
    ReqRoleDeptSaveSchema, ReqRoleDeptFindSchema
from common.entity.RoleDept import RoleDept
from common.service.RoleDeptService import RoleDeptService
from xinhou_openai_framework.core.db.DatabaseManager import DatabaseManager
from xinhou_openai_framework.core.exception.GlobalExceptionType import ParameterException
from xinhou_openai_framework.core.reponse.R import R
from xinhou_openai_framework.core.reponse.ResModel import ResModel
from xinhou_openai_framework.pages.PageHelper import PageResultHelper, PageHelper

api = APIRouter()


@api.post('/admin/roleDept/find',
          tags=["admin", "roleDept"],
          response_model=ResModel[PageResultHelper[ResRoleDeptDetailSchema]],
          summary="查询角色和部门关联表信息带分页接口",
          description="通过参数模型传递条件查询")
async def find(search: PageHelper[ReqRoleDeptFindSchema], db: Session = Depends(DatabaseManager().get_session)):
    logger.info("[index][request]:{}".format(search.model_dump()))
    return R.SUCCESS(RoleDeptService(db).find_by(search))


@api.post('/admin/roleDept/findAll',
          tags=["admin", "roleDept"],
          response_model=ResModel[PageResultHelper[ResRoleDeptDetailSchema]],
          summary="查询角色和部门关联表信息接口",
          description="通过参数模型传递条件查询")
async def find_all(search: ReqRoleDeptFindSchema, db: Session = Depends(DatabaseManager().get_session)):
    logger.info("[index][request]:{}".format(search.model_dump()))
    return R.SUCCESS(RoleDeptService(db).find_by(find_all))


@api.post("/admin/roleDept/save",
          tags=["admin", "roleDept"],
          summary="保存角色和部门关联表信息接口",
          description="通过参数模型保存数据")
async def save(model: ReqRoleDeptSaveSchema, db: Session = Depends(DatabaseManager().get_session)):
    logger.info("[save][request]:{}".format(model.json()))
    return R.SUCCESS(RoleDeptService(db).save(RoleDept(**model.dict(exclude_unset=True))))


@api.get("/admin/roleDept/delete/{id}",
         tags=["admin", "roleDept"],
         response_model=ResModel,
         summary="删除角色和部门关联表信息接口",
         description="根据ID删除数据")
async def delete(id: int = Path(title="ID不能为空"), db: Session = Depends(DatabaseManager().get_session)):
    logger.info("[detail][request]:{}".format({"id": id}))
    if id is None:
        raise ParameterException()
    RoleDeptService(db).delete(RoleDept(id=id))
    return R.SUCCESS()


@api.post("/admin/roleDept/update",
          tags=["admin", "roleDept"],
          response_model=ResModel[ResRoleDeptDetailSchema],
          summary="更新角色和部门关联表信息接口",
          description="根据ID更新数据")
async def update(model: ReqRoleDeptUpdateSchema, db: Session = Depends(DatabaseManager().get_session)):
    logger.info("[save][request]:{}".format(model.model_dump()))
    if model.id is None:
        raise ParameterException()
    return R.SUCCESS(RoleDeptService(db).update(RoleDept(**model.model_dump(exclude_unset=True))))


@api.get("/admin/roleDept/detail/{id}",
         tags=["admin", "roleDept"],
         response_model=ResModel[ResRoleDeptDetailSchema],
         summary="获取角色和部门关联表详情接口",
         description="根据ID获取数据")
async def detail(id: int = Path(title="ID不能为空"), db: Session = Depends(DatabaseManager().get_session)):
    logger.info("[detail][request]:{}".format({"id": id}))
    if id is None:
        raise ParameterException()
    return R.SUCCESS(RoleDeptService(db).find_by_id(RoleDept(id=id)))
